package com.ruoyi.common.config;

import org.eclipse.paho.mqttv5.client.MqttConnectionOptions;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * 创建MqttConnectOptions连接对象
 */
@Component
public class MqttConnect {

    @Resource
    private MqttConfig config;

    public MqttConnect(MqttConfig config) {
        this.config = config;
    }

    /**
     * 连接对象
     *
     * @return
     */
    public MqttConnectionOptions getOptions() {
        MqttConnectionOptions options = new MqttConnectionOptions();
        options.setUserName(config.getUsername());
        options.setPassword(config.getPassword().getBytes());
        options.setConnectionTimeout(config.getConnectionTimeout());
        //设置心跳
        options.setKeepAliveInterval(config.getKeepalive());
        return options;
    }

    /**
     * 连接对象
     *
     * @param options
     * @return
     */
    public MqttConnectionOptions getOptions(MqttConnectionOptions options) {
        options.setUserName(options.getUserName());
        options.setPassword(options.getPassword());
        options.setConnectionTimeout(options.getConnectionTimeout());
        options.setKeepAliveInterval(options.getKeepAliveInterval());
        return options;
    }

}
